package com.hccl.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DM.DialogManager;
import com.hccl.service.ModelUpdater;
import com.hccl.service.Parser;
import com.hccl.util.ParserPoolUtil;
import entities.SLUResult;
import entities.SysAction;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;

import com.hccl.sluentities.ResultsEntity;

@RestController
@RequestMapping("/")
public class SLUController {

	private static final Logger logger = Logger.getLogger(SLUController.class);
	@Autowired
	private ParserPoolUtil parserPoolUtil;
	@Autowired
	private DialogManager dialogManager;

	//req的编码需不需要设置？
	// 理解接口
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ResultsEntity parser(HttpServletRequest request,
								@RequestParam(name = "line", required = false) String query,
								@RequestParam(name = "greeting", required = false) String greeting,
								@RequestParam(name = "keypad", required = false) String keypad,
								@RequestParam(name = "offline", required = false) String offline,
								@RequestParam(name = "userId", required = true) String userId,
								@RequestParam(name = "debug", required = false) String debug,
								@RequestParam(name = "tel", required = true) String tel) throws Exception {

		ResultsEntity results = new ResultsEntity();
		if (offline != null && offline.equalsIgnoreCase("true")) {
			dialogManager.resetState(userId);
		}
		if (keypad==null && query==null) {
			// TODO return error code
		}
		Parser parser = null;
		String input = null;
		try {
			parser = parserPoolUtil.take();
			if (greeting != null){
				dialogManager.getFirstQusetion(userId);
			} else if (keypad != null) {
				parser.keypadParser(query, userId, tel);
				input = "keypad: " + keypad;
			} else {
				parser.queryParser(query, userId, tel);
				input = "line: " + query;
			}
			SysAction action = parser.getCurrentAction();
			ArrayList<SLUResult> sluResults = parser.getCurrentSLU();

			results.setReply(action.reply);
			results.setUserid(userId);
			results.setAction(action.stateID);
			results.setParams(action.params);
			results.setInput(input);
			results.setFunction(action.query);
			results.setKeypad(action.keypad);
			if (debug.trim().toLowerCase().equals("true")) {
				results.setDebug(sluResults);
				results.setGazetteer(parser.getCurrentGaz());
			}
		}catch (Exception e) {
			e.printStackTrace();
			if(input != null)
				System.out.println(input);
		} finally {
			if (parser != null) {
				parserPoolUtil.add(parser);
			}
		}
		return results;
	}

	// 语法更新接口
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseEntity<Object> update(HttpServletRequest request, @RequestBody(required = true) String jsonStr)
			throws Exception {
		ModelUpdater modelUpdater = new ModelUpdater();
		modelUpdater.update();// 这里需要MongoDB
		//更新成功怎么返回比较好
		return new ResponseEntity<>(jsonStr, HttpStatus.OK);

	}
}
